package com.aiyige.utils;

import android.text.TextUtils;
import android.util.Patterns;

import com.aiyige.MyApp;
import com.vondear.rxtools.RxNetTool;
import com.vondear.rxtools.RxTool;

/**
 * Created by zhuyakun on 2017/12/28.
 */

public class WebImageUrlModifier {

    public static final String WIFI_BIG_IMAGE = "imageView2/0/q/75/w/600/ignore-error/1";
    public static final String WIFI_MEDIUM_IMAGE = "imageView2/0/q/75/w/500/ignore-error/1";
    public static final String WIFI_SMALL_IMAGE = "imageView2/0/q/75/w/400/ignore-error/1";

    public static final String CELLULAR_4G_BIG_IMAGE = "imageView2/0/q/75/w/600/ignore-error/1";
    public static final String CELLULAR_4G_MEDIUM_IMAGE = "imageView2/0/q/75/w/500/ignore-error/1";
    public static final String CELLULAR_4G_SMALL_IMAGE = "imageView2/0/q/75/w/400/ignore-error/1";

    public static final String CELLULAR_3G_BIG_IMAGE = "imageView2/0/q/75/w/200/ignore-error/1";
    public static final String CELLULAR_3G_MEDIUM_IMAGE = "imageView2/0/q/75/w/100/ignore-error/1";
    public static final String CELLULAR_3G_SMALL_IMAGE = "imageView2/0/q/75/w/150/ignore-error/1";

    public static final String CELLULAR_2G_BIG_IMAGE = "imageView2/0/q/75/w/100/ignore-error/1";
    public static final String CELLULAR_2G_MEDIUM_IMAGE = "imageView2/0/q/75/w/50/ignore-error/1";
    public static final String CELLULAR_2G_SMALL_IMAGE = "imageView2/0/q/75/w/30/ignore-error/1";


    public static final String WIFI_BIG_JPG_IMAGE = "imageView2/0/format/jpg/q/75/w/600/ignore-error/1";
    public static final String WIFI_MEDIUM_JPG_IMAGE = "imageView2/0/format/jpg/q/75/w/500/ignore-error/1";
    public static final String WIFI_SMALL_JPG_IMAGE = "imageView2/0/format/jpg/q/75/w/400/ignore-error/1";

    public static final String CELLULAR_4G_BIG_JPG_IMAGE = "imageView2/0/format/jpg/q/75/w/600/ignore-error/1";
    public static final String CELLULAR_4G_MEDIUM_JPG_IMAGE = "imageView2/0/format/jpg/q/75/w/500/ignore-error/1";
    public static final String CELLULAR_4G_SMALL_JPG_IMAGE = "imageView2/0/format/jpg/q/75/w/400/ignore-error/1";

    public static final String CELLULAR_3G_BIG_JPG_IMAGE = "imageView2/0/format/jpg/q/75/w/200/ignore-error/1";
    public static final String CELLULAR_3G_MEDIUM_JPG_IMAGE = "imageView2/0/format/jpg/q/75/w/100/ignore-error/1";
    public static final String CELLULAR_3G_SMALL_JPG_IMAGE = "imageView2/0/format/jpg/q/75/w/150/ignore-error/1";

    public static final String CELLULAR_2G_BIG_JPG_IMAGE = "imageView2/0/format/jpg/q/75/w/100/ignore-error/1";
    public static final String CELLULAR_2G_MEDIUM_JPG_IMAGE = "imageView2/0/format/jpg/q/75/w/50/ignore-error/1";
    public static final String CELLULAR_2G_SMALL_JPG_IMAGE = "imageView2/0/format/jpg/q/75/w/30/ignore-error/1";

    public static final String WIFI_JPG_THUMBNAIL = "imageView2/0/format/jpg/q/50/w/80/ignore-error/1";
    public static final String CELLULAR_4G_JPG_THUMBNAIL = "imageView2/0/format/jpg/q/50/w/80/ignore-error/1";
    public static final String CELLULAR_3G_JPG_THUMBNAIL = "imageView2/0/format/jpg/q/50/w/60/ignore-error/1";
    public static final String CELLULAR_2G_JPG_THUMBNAIL = "imageView2/0/format/jpg/q/50/w/30/ignore-error/1";


    public static String bigImage(String url) {
        if (!checkWebImageUrl(url)) return url;
        String modifier = "";
        if (RxNetTool.isWifi(MyApp.getAppContext())) {
            modifier = WIFI_BIG_IMAGE;
        } else if (RxNetTool.is4G(MyApp.getAppContext())) {
            modifier = CELLULAR_4G_BIG_IMAGE;
        } else if (RxNetTool.is3rd(MyApp.getAppContext())) {
            modifier = CELLULAR_3G_BIG_IMAGE;
        } else {
            modifier = CELLULAR_2G_BIG_IMAGE;
        }
        return modify(url, modifier);
    }

    public static String mediumImage(String url) {
        if (!checkWebImageUrl(url)) return url;
        String modifier = "";
        if (RxNetTool.isWifi(MyApp.getAppContext())) {
            modifier = WIFI_MEDIUM_IMAGE;
        } else if (RxNetTool.is4G(MyApp.getAppContext())) {
            modifier = CELLULAR_4G_MEDIUM_IMAGE;
        } else if (RxNetTool.is3rd(MyApp.getAppContext())) {
            modifier = CELLULAR_3G_MEDIUM_IMAGE;
        } else {
            modifier = CELLULAR_2G_MEDIUM_IMAGE;
        }
        return modify(url, modifier);
    }

    public static String smallImage(String url) {
        if (!checkWebImageUrl(url)) return url;
        String modifier = "";
        if (RxNetTool.isWifi(MyApp.getAppContext())) {
            modifier = WIFI_SMALL_IMAGE;
        } else if (RxNetTool.is4G(MyApp.getAppContext())) {
            modifier = CELLULAR_4G_SMALL_IMAGE;
        } else if (RxNetTool.is3rd(MyApp.getAppContext())) {
            modifier = CELLULAR_3G_SMALL_IMAGE;
        } else {
            modifier = CELLULAR_2G_SMALL_IMAGE;
        }
        return modify(url, modifier);
    }

    public static String bigJpgImage(String url) {
        if (!checkWebImageUrl(url)) return url;
        String modifier = "";
        if (RxNetTool.isWifi(MyApp.getAppContext())) {
            modifier = WIFI_BIG_JPG_IMAGE;
        } else if (RxNetTool.is4G(MyApp.getAppContext())) {
            modifier = CELLULAR_4G_BIG_JPG_IMAGE;
        } else if (RxNetTool.is3rd(MyApp.getAppContext())) {
            modifier = CELLULAR_3G_BIG_JPG_IMAGE;
        } else {
            modifier = CELLULAR_2G_BIG_JPG_IMAGE;
        }
        return modify(url, modifier);
    }

    public static String mediumJpgImage(String url) {
        if (!checkWebImageUrl(url)) return url;
        String modifier = "";
        if (RxNetTool.isWifi(MyApp.getAppContext())) {
            modifier = WIFI_MEDIUM_JPG_IMAGE;
        } else if (RxNetTool.is4G(MyApp.getAppContext())) {
            modifier = CELLULAR_4G_MEDIUM_JPG_IMAGE;
        } else if (RxNetTool.is3rd(MyApp.getAppContext())) {
            modifier = CELLULAR_3G_MEDIUM_JPG_IMAGE;
        } else {
            modifier = CELLULAR_2G_MEDIUM_JPG_IMAGE;
        }
        return modify(url, modifier);
    }

    public static String smallJpgImage(String url) {
        if (!checkWebImageUrl(url)) return url;
        String modifier = "";
        if (RxNetTool.isWifi(MyApp.getAppContext())) {
            modifier = WIFI_SMALL_JPG_IMAGE;
        } else if (RxNetTool.is4G(MyApp.getAppContext())) {
            modifier = CELLULAR_4G_SMALL_JPG_IMAGE;
        } else if (RxNetTool.is3rd(MyApp.getAppContext())) {
            modifier = CELLULAR_3G_SMALL_JPG_IMAGE;
        } else {
            modifier = CELLULAR_2G_SMALL_JPG_IMAGE;
        }
        return modify(url, modifier);
    }


    public static String jpgThumbnail(String url) {
        if (!checkWebImageUrl(url)) return url;
        String modifier = "";
        if (RxNetTool.isWifi(MyApp.getAppContext())) {
            modifier = WIFI_JPG_THUMBNAIL;
        } else if (RxNetTool.is4G(MyApp.getAppContext())) {
            modifier = CELLULAR_4G_JPG_THUMBNAIL;
        } else if (RxNetTool.is3rd(MyApp.getAppContext())) {
            modifier = CELLULAR_3G_JPG_THUMBNAIL;
        } else {
            modifier = CELLULAR_2G_JPG_THUMBNAIL;
        }
        return modify(url, modifier);
    }


    private static String modify(String originalUrl, String modifier) {
        if (originalUrl.contains("?")) {
            return originalUrl;
        } else {
            return originalUrl + "?" + modifier;
        }
    }

    private static boolean checkWebImageUrl(String url) {
        return !TextUtils.isEmpty(url) && Patterns.WEB_URL.matcher(url).matches();
    }

}
